import aioredis

from .base import DatabaseProxy


class AioRedisDatabaseProxy(DatabaseProxy):
    def __init__(self, url, **kwargs):
        self.url = url
        self.pool_kwargs = kwargs
        self.pool = None

    @property
    def raw(self):
        return self.pool

    async def initialize(self):
        self.pool = await aioredis.create_pool(
            self.url,
            **self.pool_kwargs,
        )

    async def get_connection(self):
        return await self.pool.acquire()

    async def close_connection(self, connection):
        return self.pool.release(connection)
